package com.secxiot.consumer.user.controller;

import cn.hutool.crypto.SecureUtil;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.exception.BaseException;
import com.secxiot.common.exception.BaseRuntimeException;
import com.secxiot.common.utils.CachedBeanCopier;
import com.secxiot.common.utils.ClientUnit;
import com.secxiot.common.utils.JSMSUtil;
import com.secxiot.consumer.system.service.SystemConsumerService;
import com.secxiot.consumer.user.feign.UserClient;
import com.secxiot.provider.system.dto.AppSchedulDto;
import com.secxiot.provider.system.dto.FileLogDto;
import com.secxiot.provider.system.entity.AppPunchCard;
import com.secxiot.provider.system.vo.AppPunchCardVo;
import com.secxiot.provider.system.vo.AppSchedulVo;
import com.secxiot.provider.system.vo.UserTypeVo;
import com.secxiot.provider.task.vo.TaskStatusVo;
import com.secxiot.provider.user.dto.DeptDto;
import com.secxiot.provider.user.dto.UserDeptDto;
import com.secxiot.provider.user.dto.UserDto;
import com.secxiot.provider.user.dto.UserNarcoticDto;
import com.secxiot.provider.user.entity.UserAccount;
import com.secxiot.provider.user.entity.UserDo;
import com.secxiot.provider.user.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"用户模块"})
@RestController
@RequestMapping("/users")
public class UserConsumerController {
    private static final Logger log = LoggerFactory.getLogger(UserConsumerController.class);

    @Autowired
    private UserClient userClient;

    @Autowired
    private SystemConsumerService systemConsumerService;

    @ApiOperation(value = "根据id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping
    public ResultVo<UserVo> getUser(@RequestParam(value = "userId", required = true) Long userId) {
        UserDo user = userClient.getUser(userId);
        if (user == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NOT_FOUND);
        }
        UserVo userVo = new UserVo();
        CachedBeanCopier.copy(user, userVo);
        userVo.setStatus(user.getStatus());
        userVo.setSex(user.getSex());
        DeptDto dept = userClient.getDeptByUserId(userId);
        if (dept != null) {
            userVo.setDeptName(dept.getName());
            userVo.setDeptId(dept.getDeptId());
        }
        FileLogDto fileUri = userClient.getFileUri(user.getPicId());
        if (fileUri != null) {
            userVo.setHeaderImg(fileUri.getFileUri());
        }
        if (!"0000100001".equals(user.getType())) {
            List<UserDeptDto> userDeptDtos = userClient.listSupperDeptByUserId(user.getUserId());
            List<DeptVo> supperDepts = new ArrayList<>();
            for (int i = 0; i < userDeptDtos.size(); i++) {
                UserDeptDto d = userDeptDtos.get(i);
                DeptVo deptVo = new DeptVo();
                CachedBeanCopier.copy(d, deptVo);
                supperDepts.add(deptVo);
            }
            userVo.setSupperDepts(supperDepts);
        }
        return new ResultVo<>(userVo);
    }

    @ApiOperation(value = "获取病患列表接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/patient")
    public ResultVo<List<PatientVo>> getPatientList(@RequestParam(value = "deptId", required = true) Long deptId) {
        List<PatientVo> list = userClient.getPatientList(deptId);
        return new ResultVo<List<PatientVo>>(list);
    }

    @ApiOperation(value = "验证旧手机号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "mobile", value = "旧手机号", paramType = "form", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", paramType = "form", required = true)
    })
    @PostMapping("/mobile/valid")
    public ResultVo<Boolean> validMobile(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "code", required = true) String code,
            HttpServletRequest request) throws APIConnectionException, APIRequestException, BaseException {

        UserDo userDo = userClient.getUser(userId);
        if (!mobile.equals(userDo.getMobile())) {
            return new ResultVo<Boolean>(ServerStatusEnum.VALID_SMS_CODE_ERROR);
        }
        if (!JSMSUtil.sendValidSMSCode(mobile, code)) {
            return new ResultVo<Boolean>(ServerStatusEnum.VALID_SMS_CODE_ERROR);
        }
        return new ResultVo<Boolean>(true);
    }

    @ApiOperation(value = "更换手机号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "mobile", value = "新手机号", paramType = "form", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", paramType = "form", required = true)
    })
    @PostMapping("/mobile")
    public ResultVo<UserVo> changeMobile(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "code", required = true) String code,
            HttpServletRequest request) throws APIConnectionException, APIRequestException, BaseException {

        if (!JSMSUtil.sendValidSMSCode(mobile, code)) {
            return new ResultVo<UserVo>(ServerStatusEnum.VALID_SMS_CODE_ERROR);
        }

        UserDto userDto = UserDto.builder()
                .userId(userId)
                .username(mobile)
                .mobile(mobile)
                .build();

        //更新手机号码
        UserDo update = userClient.update(userDto);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(update, userVo);
        return new ResultVo<UserVo>(userVo);
    }

    @ApiOperation(value = "更换密码-旧密码验证", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "form", required = true),
            @ApiImplicitParam(name = "passwordNew", value = "新密码", paramType = "form", required = true)
    })
    @PostMapping("/password")
    public ResultVo<String> changePassword(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "password", required = true) String password,
            @RequestParam(value = "passwordNew", required = true) String passwordNew,
            HttpServletRequest request) {

        /*        ResultVo<UserDo> userDo = new ResultVo<>(userClient.getUser(userId));*/
        UserDo userDo = userClient.getUser(userId);
        // 登录校验
        if (userDo == null) {
            return new ResultVo<String>(ServerStatusEnum.NOT_FOUND);
        }
        String md5 = SecureUtil.md5(password);
        if (userDo.getPassword().equalsIgnoreCase(md5)) {
            UserDto userDto = UserDto.builder()
                    .userId(userId)
                    .password(SecureUtil.md5(passwordNew))
                    .build();
            userClient.update(userDto);
            return new ResultVo<String>("SUCCESS");
        } else {
            return new ResultVo<String>(ServerStatusEnum.LOGIN_FAIL);
        }
    }

    @ApiOperation(value = "更换密码-验证码验证", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "code", value = "验证码", paramType = "form", required = true),
            @ApiImplicitParam(name = "passwordNew", value = "新密码", paramType = "form", required = true)
    })
    @PostMapping("/password/code")
    public ResultVo<String> changePasswordByCode(
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "passwordNew", required = true) String passwordNew,
            HttpServletRequest request) throws APIConnectionException, APIRequestException, BaseException {

        UserDo userDo = userClient.getUserByPhone(mobile, ClientUnit.CLIENT_TYPE_APP);
        // 登录校验
        if (userDo == null) {
            return new ResultVo<String>(ServerStatusEnum.NOT_FOUND);
        }
        if (!JSMSUtil.sendValidSMSCode(userDo.getMobile(), code)) {
            return new ResultVo<String>(ServerStatusEnum.VALID_SMS_CODE_ERROR);
        }
        UserDto userDto = UserDto.builder()
                .userId(userDo.getUserId())
                .password(SecureUtil.md5(passwordNew))
                .build();
        //更新手机号码
        userClient.update(userDto);
        return new ResultVo<String>("SUCCESS");
    }

    @ApiOperation(value = "添加紧急联系人", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "relation", value = "关系", paramType = "form", required = true),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "form", required = false),
            @ApiImplicitParam(name = "mobile", value = "手机号", paramType = "form", required = true)
    })
    @PostMapping("/contact")
    public ResultVo<UserEmergencyContactVo> addContact(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "relation", required = true) String relation,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "mobile", required = true) String mobile,
            HttpServletRequest request) {

        /*        ResultVo<UserDo> userDo = new ResultVo<>(userClient.getUser(userId));*/
        UserDo userDo = userClient.getUser(userId);
        // 登录校验
        if (userDo == null) {
            return new ResultVo<UserEmergencyContactVo>(ServerStatusEnum.NOT_FOUND);
        } else {
            return new ResultVo<UserEmergencyContactVo>(userClient.addContact(userId, relation, name, mobile));
        }
    }

    @ApiOperation(value = "获取紧急联系人", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/contact")
    public ResultVo<UserEmergencyContactVo> addContact(
            @RequestParam(value = "userId", required = true) Long userId) {
        UserDo userDo = userClient.getUser(userId);
        // 登录校验
        if (userDo == null) {
            return new ResultVo<>(ServerStatusEnum.NOT_FOUND);
        } else {
            return new ResultVo<UserEmergencyContactVo>(userClient.getContact(userId));
        }
    }

    @ApiOperation(value = "更换头像", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "fileLogId", value = "文件日志id", paramType = "form", required = true)
    })
    @PostMapping("/headerImg")
    public ResultVo<String> changePasswordByCode(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "fileLogId", required = true) Long fileLogId
    ) throws APIConnectionException, APIRequestException, BaseException {

        UserDo userDo = userClient.getUser(userId);
        // 登录校验
        if (userDo == null) {
            return new ResultVo<String>(ServerStatusEnum.NOT_FOUND);
        }
        UserDto userDto = UserDto.builder()
                .userId(userId)
                .picId(fileLogId)
                .build();
        //更新手机号码
        userClient.update(userDto);
        ArrayList<Long> files = new ArrayList<>();
        files.add(fileLogId);
        userClient.useFileLogs(files);
        return new ResultVo<String>("SUCCESS");
    }

    @ApiOperation(value = "获取任务接收者列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userTypes", value = "用户类型列表", required = false, paramType = "query", allowMultiple = true),
            @ApiImplicitParam(name = "taskType", value = "任务类型", required = true, paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "部门id", required = false, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "directFlag", value = "是否只查询直属部门人员，1:是 0:否", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "isVisit", value = "是否出诊", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/list")
    public ResultVo<ReceiverListVo> getUserSimpleList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "userTypes", required = false) String[] userTypes,
            @RequestParam(value = "taskType", required = true) String taskType,
            @RequestParam(value = "deptId", required = false) Long deptId,
            @RequestParam(value = "directFlag", required = true) Integer directFlag,
            @RequestParam(value = "isVisit", required = false) String isVisit,
            HttpServletRequest request) {
        ReceiverListVo receiverList = new ReceiverListVo();
        List<DeptVo> depts = receiverList.getDepts();
        List<UserSimpleVo> users = receiverList.getUsers();

        UserDo user = userClient.getUser(userId);
        if (user.getDeptId() == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NO_DEPT);
        }

        List<UserTypeVo> userTypeList = systemConsumerService.getUserTypeList(userId, taskType);
        //若未筛选用户类型，则未符合条件的所有用户类型，若已筛选则是指定类型
        List<String> types = new ArrayList<>();
        if (userTypes == null || userTypes.length < 1) {
            userTypeList.stream().map(u -> u.getUserTypes()).forEach(t -> types.addAll(t));
        } else {
            types.addAll(Arrays.asList(userTypes));
        }

        //部门范围
        DeptDto userDept = userClient.getDeptByDeptId(user.getDeptId());
        if (userDept == null) {
            return new ResultVo<>(ServerStatusEnum.NO_DEPT);
        }
        String[] supperIds = userDept.getSupperIds().split("-");
        int deptLevel = 1;
        int userDeptLevel = userDept.getDeptLevel();
        if (deptId == null) {
            if (userDept.getParentId() == 0) {
                //医院层级，默认医院id
                deptId = user.getDeptId();
            } else {
                deptId = Long.parseLong(supperIds[1]);
            }
        } else {
            deptLevel = userClient.getDeptByDeptId(deptId).getDeptLevel();
        }

        if (directFlag == 0) {
            directFlag = null;
        }
        if (deptLevel < userDeptLevel - 1) {
            //条件部门级别高于用户归属部门级别，则只返回条件部门下级部门中该用户所归属的部门
            DeptDto deptDto = userClient.getDeptByDeptId(Long.parseLong(supperIds[deptLevel + 1]));
            DeptVo deptVo = new DeptVo(deptDto);
            depts.add(deptVo);
            if (deptLevel + 1 == userDeptLevel) {
                List<UserSimpleVo> collect = userClient.listSubManagers(deptId)
                        .stream().filter(u -> u.getUserId() != userId).collect(Collectors.toList());
                users.addAll(collect);
            }
            if (directFlag == null) {
                List<UserSimpleVo> pene = userClient.getUserSimpleList(userId, types, userDept.getDeptId(), directFlag, taskType);
                users.addAll(pene);
            }
        } else if (deptLevel == userDeptLevel - 1) {
            //条件部门级别高于用户归属部门级别，则只返回条件部门下级部门中该用户所归属的部门
            DeptDto deptDto = userClient.getDeptByDeptId(userDept.getDeptId());
            DeptVo deptVo = new DeptVo(deptDto);
            depts.add(deptVo);
            if (userId.equals(userDept.getManagerId())) {
                //管理者可以看到同级别管理者
                List<UserSimpleVo> collect = userClient.listSubManagers(deptId)
                        .stream().filter(u -> u.getUserId() != userId).collect(Collectors.toList());
                users.addAll(collect);
            }
            if (directFlag == null) {
                List<UserSimpleVo> pene = userClient.getUserSimpleList(userId, types, userDept.getDeptId(), directFlag, taskType);
                users.addAll(pene);
            }
        } else {
            List<DeptDto> deptDtos = userClient.listSubdeptByDeptId(deptId);
            List<DeptVo> collect = deptDtos.stream().map(deptDto -> new DeptVo(deptDto)).collect(Collectors.toList());
            depts.addAll(collect);
            List<UserSimpleVo> pene = userClient.getUserSimpleList(userId, types, deptId, directFlag, taskType);
            users.addAll(pene);
        }

        for (UserSimpleVo userSimpleVo : users) {
            List<TaskStatusVo> taskStatusList = userClient.getTaskByReceiverId(userSimpleVo.getUserId());
            if (CollectionUtils.isEmpty(taskStatusList)) {
                userSimpleVo.setTaskStatus("true");
            } else {
                userSimpleVo.setTaskStatus("false");
            }
        }
        return new ResultVo<ReceiverListVo>(receiverList);
    }

    @ApiOperation(value = "修改用户信息", httpMethod = "POST")
    @PostMapping("/updateSysUser")
    public ResultVo<UserDo> updateSysUser(@RequestBody UserNarcoticDto userNarcoticDto) {
        UserDo userDo = userClient.getUser(userNarcoticDto.getUserId());
        // 登录校验
        if (userDo == null) {
            return new ResultVo<>(ServerStatusEnum.NOT_FOUND);
        }

        UserDto userDto = UserDto.builder().build();
        CachedBeanCopier.copy(userNarcoticDto, userDto);
        return new ResultVo<UserDo>(userClient.update(userDto));
    }

    @ApiOperation(value = "考勤打卡", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true),
            @ApiImplicitParam(name = "longitude", value = "经度", paramType = "form", required = true),
            @ApiImplicitParam(name = "latitude", value = "纬度", paramType = "form", required = true),
            @ApiImplicitParam(name = "address", value = "详细地址", paramType = "form", required = true)
    })
    @PostMapping("/addPunchCard")
    public ResultVo<AppPunchCardVo> addPunchCard(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "longitude", required = true) BigDecimal longitude,
            @RequestParam(value = "latitude", required = true) BigDecimal latitude,
            @RequestParam(value = "address", required = true) String address, HttpServletRequest request) throws ParseException {
        UserDo user = userClient.getUser(userId);
        if (user.getDeptId() == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NO_DEPT);
        }

        if (user.getHospitalId() == null) {
            throw new BaseRuntimeException(ServerStatusEnum.TYPE_NOTHOS);
        }
        HospitalVo hospitalVo = userClient.getNearestHospital(longitude, latitude, user.getHospitalId());
        if (null == hospitalVo) {
            throw new BaseRuntimeException(ServerStatusEnum.TYPE_NOTHOS);
        }

        Date date = new Date();
        String punchType = "0001700001";
        AppPunchCardVo appPunchCardVo = userClient.getPunchCard(userId, user.getHospitalId(), punchType, date);
        if (null != appPunchCardVo) {
            punchType = "0001700002";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(DateFormatUtils.format(date, "yyyy-MM-dd 09:00:00"));
        Date endTime = sdf.parse(DateFormatUtils.format(date, "yyyy-MM-dd 18:00:00"));
        int isAccord = 1;
        if (punchType.equals("0001700001")) {
            if (!date.before(startTime) || hospitalVo.getDistance() > 30000) {
                isAccord = 0;
            }
        } else {
            if (!date.after(endTime) || hospitalVo.getDistance() > 30000) {
                isAccord = 0;
            }
        }

        AppPunchCard appPunchCard = AppPunchCard.builder()
                .userId(userId)
                .hospitalId(user.getHospitalId())
                .deptId(user.getHospitalId())
                .punchTime(date)
                .punchType(punchType)
                .longitude(longitude)
                .latitude(latitude)
                .distance(BigDecimal.valueOf(hospitalVo.getDistance()))
                .address(address)
                .isAccord(isAccord)
                .status(1)
                .build();
        userClient.addPunchCard(appPunchCard);
        AppPunchCardVo punchCardVo = new AppPunchCardVo();
        CachedBeanCopier.copy(appPunchCard, punchCardVo);
        return new ResultVo<AppPunchCardVo>(punchCardVo);
    }

    @ApiOperation(value = "获取考勤记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "dateTime", value = "时间", required = true, paramType = "query", dataType = "date")
    })
    @GetMapping("/punchCardList")
    public ResultVo<List<AppPunchCardVo>> getAppRecommend(@RequestParam(value = "userId", required = true) Long userId,
                                                          @RequestParam(value = "hospitalId", required = true) Long hospitalId,
                                                          @RequestParam(value = "dateTime", required = true) Date dateTime
    ) {
        List<AppPunchCardVo> punchCardList = userClient.getPunchCardList(userId, hospitalId, dateTime);
        return new ResultVo<>(punchCardList);
    }

    @ApiOperation(value = "创建排班", httpMethod = "POST")
    @PostMapping("/schedul/add")
    public Integer addAppSchedul(@RequestBody AppSchedulDto appSchedulDto) {
        if (null == appSchedulDto) {
            throw new BaseRuntimeException(ServerStatusEnum.PARAM_WRONG);
        }
        if (null == appSchedulDto.getUserId()) {
            throw new BaseRuntimeException(ServerStatusEnum.NOT_DATA_FOUND);
        }
        if (null == appSchedulDto.getStartTime()) {
            throw new BaseRuntimeException(ServerStatusEnum.TIME_IS_NOT);
        }
        if (null == appSchedulDto.getEndTime()) {
            throw new BaseRuntimeException(ServerStatusEnum.TIME_IS_NOT);
        }

        if (CollectionUtils.isEmpty(appSchedulDto.getAppSchedulRecordDto())) {
            throw new BaseRuntimeException(ServerStatusEnum.SCHEDUL_RECORD_EMPTY);
        }
        return userClient.addAppSchedul(appSchedulDto);
    }

    @ApiOperation(value = "获取排班列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", paramType = "form", required = true)
    })
    @GetMapping("/schedul/List")
    public ResultVo<List<AppSchedulVo>> getAppSchedulList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId) {
        List<AppSchedulVo> appSchedulVoList = userClient.getAppSchedulList(userId, hospitalId);
        return new ResultVo<List<AppSchedulVo>>(appSchedulVoList);
    }

    /*@ApiOperation(value = "获取排班记录列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", paramType = "form", required = true),
            @ApiImplicitParam(name = "schedulId", value = "排班id", paramType = "form", required = true)
    })
    @GetMapping("/schedul/recordList")
    public ResultVo<List<AppSchedulRecordVo>> getAppSchedulRecordList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "schedulId", required = true) Long schedulId) {
        List<AppSchedulRecordVo> appSchedulRecordVoList = userClient.getAppSchedulRecordList(userId, hospitalId, schedulId);
        return new ResultVo<List<AppSchedulRecordVo>>(appSchedulRecordVoList);
    }*/

    @ApiOperation(value = "获取分享用户列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Long")
    })
    @PostMapping("/getPromoteUserList")
    public ResultVo<List<UserVo>> getPromoteUserList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "pageNo", required = true) Long pageNo,
            @RequestParam(value = "pageSize", required = true) Long pageSize,
            HttpServletRequest request) {
        return new ResultVo<List<UserVo>>(userClient.getPromoteUserList(userId, pageNo, pageSize));
    }

    @ApiOperation(value = "绑定邀请码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "promoteCode", value = "推广码", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/bingPromoteCode")
    public Integer bingPromoteCode(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "promoteCode", required = true) String promoteCode,
            HttpServletRequest request) {
        return userClient.bingPromoteCode(userId, promoteCode);
    }

    @ApiOperation(value = "获取用户提现账户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true, dataType = "Long")
    })
    @GetMapping("/getUserAccount")
    public ResultVo<UserAccount> getUserAccount(@RequestParam(value = "userId", required = true) Long userId) {
        return new ResultVo<>(userClient.getUserAccount(userId));
    }

    @ApiOperation(value = "绑定提现账户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "type", value = "账户类型(1;微信,2支付宝)", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "payAccount", value = "提现账号", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/bingUserAccount")
    public Integer bingUserAccount(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "type", required = true) Integer type,
            @RequestParam(value = "payAccount", required = true) String payAccount,
            HttpServletRequest request) {
        return userClient.bingUserAccount(userId, type, payAccount);
    }

    @ApiOperation(value = "编辑提现账户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "账户类型(1;微信,2支付宝)", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "payAccount", value = "提现账号", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/editUserAccount")
    public Integer editUserAccount(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "type", required = true) Integer type,
            @RequestParam(value = "payAccount", required = true) String payAccount,
            HttpServletRequest request) throws APIConnectionException, APIRequestException, BaseException {
        UserDo user = userClient.getUser(userId);
        // 校验code
        if (!JSMSUtil.sendValidSMSCode(user.getMobile(), code)) {
            throw new BaseRuntimeException(ServerStatusEnum.VALID_SMS_CODE_ERROR);
        }
        return userClient.editUserAccount(userId, type, payAccount);
    }


}
